package com.lemon.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lemon.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;

/**
 * 通用 ServiceImpl
 * 封装服务层最基础的增、删、查、改等操作
 *
 * @author ymh
 * @date 2019-2-26 23:21
 */
@Transactional
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    protected Mapper<T> mapper;

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @Override
    public T get(Serializable id) {
        return mapper.selectByPrimaryKey(id);
    }

    /**
     * 查询1条数据
     *
     * @param entity
     * @return
     */
    @Override
    public T selectOne(T entity) {
        return mapper.selectOne(entity);
    }

    /**
     * 查询多条数据
     *
     * @param entity
     * @return
     */
    @Override
    public List<T> select(T entity) {
        return mapper.select(entity);
    }

    /**
     * 查询所有
     */
    @Override
    public List<T> selectAll() {
        return mapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param entity
     * @return
     */
    @Override
    public PageInfo<T> selectPage(int pageNum, int pageSize, T entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<T> list = mapper.select(entity);
        return new PageInfo<>(list);
    }

    /**
     * 分页查询
     *
     * @param pageInfo
     * @return
     */
    @Override
    public PageInfo<T> selectPage(PageInfo<T> pageInfo) {
        PageInfo<T> result = selectPage(pageInfo.getPageNum(), pageInfo.getPageSize(), null);
        pageInfo.setSize(result.getSize());
        pageInfo.setStartRow(result.getStartRow());
        pageInfo.setEndRow(result.getEndRow());
        pageInfo.setPages(result.getPages());
        pageInfo.setPrePage(result.getPrePage());
        pageInfo.setNextPage(result.getNextPage());
        pageInfo.setIsFirstPage(result.isIsFirstPage());
        pageInfo.setIsLastPage(result.isIsLastPage());
        pageInfo.setHasPreviousPage(result.isHasPreviousPage());
        pageInfo.setHasNextPage(result.isHasNextPage());
        pageInfo.setNavigatePages(result.getNavigatePages());
        pageInfo.setNavigatepageNums(result.getNavigatepageNums());
        pageInfo.setNavigateFirstPage(result.getNavigateFirstPage());
        pageInfo.setNavigateLastPage(result.getNavigateLastPage());
        pageInfo.setTotal(result.getTotal());
        pageInfo.setList(result.getList());

        return pageInfo;
    }

    /**
     * 新增
     *
     * @param entity
     */
    @Override
    public int insert(T entity) {
        Class clazz = entity.getClass();
        try {
            Field field = clazz.getDeclaredField("created");
            field.setAccessible(true);
            field.set(entity, new Date().getTime());
        } catch (Exception e) {
            throw new RuntimeException(clazz.getSimpleName() + "缺少created属性");
        }
        return mapper.insert(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @Override
    public int update(T entity) {
        Class clazz = entity.getClass();
        try {
            Field field = clazz.getDeclaredField("updated");
            field.setAccessible(true);
            field.set(entity, new Date().getTime());
        } catch (Exception e) {
            throw new RuntimeException(clazz.getSimpleName() + "缺少updated属性");
        }
        return mapper.updateByPrimaryKeySelective(entity);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Serializable id) {
        return mapper.deleteByPrimaryKey(id);
    }

    /**
     * 删除
     *
     * @param entity
     * @return
     */
    @Override
    public int delete(T entity) {
        return mapper.delete(entity);
    }

}
